package org.dromara.business.service.impl;

import org.dromara.business.domain.UserInfoWeb;
import org.dromara.business.domain.vo.UserInfoWebVo;
import org.dromara.business.mapper.UserInfoMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.mapper.SysOssMapper;
import org.dromara.system.service.ISysOssService;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.UserSkinInfoBo;
import org.dromara.business.domain.vo.UserSkinInfoVo;
import org.dromara.business.domain.UserSkinInfo;
import org.dromara.business.mapper.UserSkinInfoMapper;
import org.dromara.business.service.IUserSkinInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户主页皮肤配置Service业务层处理
 *
 * @author Hao Tong Xue
 * @date 2025-06-10
 */
@RequiredArgsConstructor
@Service
public class UserSkinInfoServiceImpl implements IUserSkinInfoService {

    private final UserSkinInfoMapper baseMapper;

    private final UserInfoMapper userInfoMapper;

    /**
     * 查询用户主页皮肤配置
     *
     * @param id 主键
     * @return 用户主页皮肤配置
     */
    @Override
    public UserSkinInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户主页皮肤配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户主页皮肤配置分页列表
     */
    @Override
    public TableDataInfo<UserSkinInfoVo> queryPageList(UserSkinInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserSkinInfo> lqw = buildQueryWrapper(bo);
        Page<UserSkinInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户主页皮肤配置列表
     *
     * @param bo 查询条件
     * @return 用户主页皮肤配置列表
     */
    @Override
    public List<UserSkinInfoVo> queryList(UserSkinInfoBo bo) {
        LambdaQueryWrapper<UserSkinInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserSkinInfo> buildQueryWrapper(UserSkinInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserSkinInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(UserSkinInfo::getId);
        lqw.like(StringUtils.isNotBlank(bo.getSkinName()), UserSkinInfo::getSkinName, bo.getSkinName());
        lqw.eq(bo.getIsFree() != null, UserSkinInfo::getIsFree, bo.getIsFree());
        lqw.eq(bo.getSkinType() != null, UserSkinInfo::getSkinType, bo.getSkinType());
        return lqw;
    }

    /**
     * 新增用户主页皮肤配置
     *
     * @param bo 用户主页皮肤配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserSkinInfoBo bo) {
        UserSkinInfo add = MapstructUtils.convert(bo, UserSkinInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户主页皮肤配置
     *
     * @param bo 用户主页皮肤配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserSkinInfoBo bo) {
        UserSkinInfo update = MapstructUtils.convert(bo, UserSkinInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserSkinInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户主页皮肤配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        List<UserSkinInfoVo> userSkinInfoVos = baseMapper.selectVoByIds(ids);
        if (userSkinInfoVos.isEmpty()) {
            return true;
        }
        userInfoMapper.selectList(Wrappers.<UserInfoWeb>lambdaQuery().in(UserInfoWeb::getSkinId, ids))
                .forEach(userInfo -> {
                    throw new RuntimeException("皮肤已被使用，无法删除");
                });

        return baseMapper.deleteByIds(ids) > 0;
    }
}
